﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_menu_sanpham : Page
{
    dulieuDataContext db = new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            LoadMenu();
            Panel1.Visible = false;
        }
        //else
        //{
        //    LoadMenu();
        //}
    }
    protected void AddMenu(object s, ImageClickEventArgs e)
    {
        Cate cateNews = new Cate() { Parent = 0, Name = txtmenu.Text, Status = true, TypeCateID = 2 };
        db.Cates.InsertOnSubmit(cateNews);
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Thêm thành công";
        LoadMenu();
    }
    protected void UpdateMenu(object s, ImageClickEventArgs e)
    {
        var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(Session["idmenu"]) select o).SingleOrDefault();
        if (cate != null) cate.Name = txtmenu.Text;
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        LoadMenu();
        lblthongbao.Text = "Cập nhật thành công";
    }

    protected void LoadMenu()
    {
        rptmnu.DataSource = from o in db.Cates where o.Parent==0&&o.TypeCateID == 2 select o;
        rptmnu.DataBind();
    }
    protected void UpdateCheck(object s, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            Label lblid = (Label)e.Item.FindControl("lblid");
            Session.Add("idmenu", lblid.Text);
            var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
            if (cate != null) txtmenu.Text = cate.Name;
            System.Threading.Thread.Sleep(800);
            
            LoadMenuCon();
            Panel1.Visible = true;
            LoadMenu();
        }
    }
    protected void DeleteMenu(object s, ImageClickEventArgs e)
    {
        foreach (RepeaterItem item in rptmnu.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chk");
            if (chk.Checked)
            {
                Label lblid = (Label)item.FindControl("lblid");
                var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
                if (cate != null) db.Cates.DeleteOnSubmit(cate);
                db.SubmitChanges();
            }
        }
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Xóa thành công";
        LoadMenu();
    }

    // các hàm cho menu con
    protected void AddMenuCon(object s, ImageClickEventArgs e)
    {
        Cate cateNews = new Cate() { Parent = Convert.ToInt32(Session["idmenu"]), Name = txtmenucon.Text, Status = true, TypeCateID = 2 };
        db.Cates.InsertOnSubmit(cateNews);
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        lblthongbao1.Text = "Thêm thành công";
        LoadMenuCon();
    }
    protected void UpdateMenuCon(object s, ImageClickEventArgs e)
    {
        var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(Session["idmenucon"]) select o).SingleOrDefault();
        if (cate != null) cate.Name = txtmenucon.Text;
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        LoadMenuCon();
        lblthongbao1.Text = "Cập nhật thành công";
    }

    protected void LoadMenuCon()
    {
        rptmnucon.DataSource = from o in db.Cates where (o.TypeCateID == 2) && (o.Parent == Convert.ToInt32(Session["idmenu"])) select o;
        rptmnucon.DataBind();
    }
    protected void UpdateCheckCon(object s, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            Label lblid = (Label)e.Item.FindControl("lblidcon");
            Session.Add("idmenucon", lblid.Text);
            var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
            if (cate != null) txtmenucon.Text = cate.Name;
            System.Threading.Thread.Sleep(800);
            LoadMenuCon();
        }
        else if(e.CommandName=="delete")
        {
            Label lblid = (Label)e.Item.FindControl("lblidcon");
            var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
            db.Cates.DeleteOnSubmit(cate);
            db.SubmitChanges();
            System.Threading.Thread.Sleep(800);
            LoadMenuCon();
        }
    }
    protected void DeleteMenuCon(object s, ImageClickEventArgs e)
    {
        foreach (RepeaterItem item in rptmnu.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkcon");
            if (chk.Checked)
            {
                Label lblid = (Label)item.FindControl("lblidcon");
                var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
                if (cate != null) db.Cates.DeleteOnSubmit(cate);
                db.SubmitChanges();
            }
        }
        System.Threading.Thread.Sleep(800);
        lblthongbao1.Text = "Xóa thành công";
        LoadMenuCon();
    }
}